#include "camwheelform.h"
#include "ui_camwheelform.h"
#include"data.h"
#include<QtMath>
#include<QHeaderView>
#include<QTimer>
#include<QMessageBox>

CamWheelForm::CamWheelForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CamWheelForm)
{
    ui->setupUi(this);

    connect(ui->pushButton_start,&QPushButton::clicked,this,[=](){
        on_button_start_clicked();
    });
    connect(ui->pushButton_stop,&QPushButton::clicked,this,[=](){
        on_button_stop_clicked();
    });

    QTimer *timer1=new QTimer;
    connect(timer1,&QTimer::timeout,this,&CamWheelForm::updateWin);
    timer1->start(100);


}

CamWheelForm::~CamWheelForm()
{
    delete ui;
}


void CamWheelForm::on_button_start_clicked(){
    for(int i=0;i<2;++i){
        int enable=0;
        PMC_GetAxisEnable(Data::handle,i,&enable);
        if(enable==0){
            QMessageBox::about(NULL,"Warn","AXIS0,1未使能，请使能！！！");
            return;
        }

    }
    runflage=true;
    ui->pushButton_start->setEnabled(false);
    ui->pushButton_start->setText("运动中");
    ui->pushButton_start->setStyleSheet("background-color:rgb(0,255,0");
    PMC_SetAtype(Data::handle,(int)ui->spinBox_axis0->value(),1);
    PMC_SetAtype(Data::handle,(int)ui->spinBox_axis1->value(),1);
    PMC_SetUnits(Data::handle,(int)ui->spinBox_axis0->value(),100);
    PMC_SetUnits(Data::handle,(int)ui->spinBox_axis1->value(),100);
    PMC_SetSpeed(Data::handle,(int)ui->spinBox_axis0->value(),(float)ui->spinBox_movesp->value());
    PMC_SetSpeed(Data::handle,(int)ui->spinBox_axis1->value(),(float)ui->spinBox_movesp->value());

    for(int j=0;j<4;++j){
        for(int i=0;i<256;++i){
             PMC_SetTable(Data::handle,i+j*256,1,&setTableValue[i+j*256]);   //把凸轮表数据写入table寄存器值
        }
    }

    //QThread::sleep(10);
    //参考AXIS 1 运动到 100 位置时，跟随轴AXIS 0 启动凸轮表运动
    PMC_Cambox(Data::handle,(int)ui->spinBox_axis1->value(),0,1024,100,(float)ui->spinBox_movedis->value(),(int)ui->spinBox_axis0->value(),6,0);
    PMC_Single_Move(Data::handle,(int)ui->spinBox_axis0->value(),(float)ui->spinBox_movedis->value());
}

void CamWheelForm::on_button_stop_clicked(){

    if(Data::handle!=nullptr){
        PMC_Rapidstop(Data::handle,3);
        runflage=false;
        ui->pushButton_start->setEnabled(true);
        ui->pushButton_start->setText("运动");
        ui->pushButton_start->setStyleSheet("background-color: rgb(255,255,255)");
    }

}

void CamWheelForm::showEvent(QShowEvent *event){
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setRowCount(1024);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"编号"<<"位置");

    //正弦曲线数据写入
    for(int j=0;j<4;++j){
        for(int i=0;i<256;++i){
            setTableValue[i+j*256]=(sin(((2*M_PI)/34)*i))*100;
            ui->tableWidget->setItem((i+j*256),0,new QTableWidgetItem(QString::number( i+j*256)));
            ui->tableWidget->setItem((i+j*256),1,new QTableWidgetItem(QString::number( setTableValue[i+j*256],'f',5)));

        }
    }

}

//窗口更新
void CamWheelForm::updateWin(){

    if(Data::handle!=nullptr){
        float dpos0=0;
        float dpos1=0;
        PMC_GetDpos(Data::handle,(int)ui->spinBox_axis0->value(),&dpos0);
        PMC_GetDpos(Data::handle,(int)ui->spinBox_axis1->value(),&dpos1);
        ui->label_axis0->setText(QString("主轴：%1").arg(dpos0));
        ui->label_axis1->setText(QString("跟随轴：%1").arg(dpos1));

        if(runflage){
            int value[2]={0,0};
            PMC_GetIfIdle(Data::handle,(int)ui->spinBox_axis0->value(),&value[0]);
            PMC_GetIfIdle(Data::handle,(int)ui->spinBox_axis1->value(),&value[1]);

            if(value[0]==-1&&value[1]==-1){
                runflage=false;
                ui->pushButton_start->setEnabled(true);
                ui->pushButton_start->setText("运动");
                ui->pushButton_start->setStyleSheet("background-color: rgb(255,255,255)");
            }
        }

    }

}


